package com.xuecheng.search.controller;

import com.xuecheng.api.course.EsCourseControllerApi;
import com.xuecheng.framework.domain.cms.response.QueryResponseResult;
import com.xuecheng.framework.domain.course.CoursePub;
import com.xuecheng.framework.domain.course.TeachplanMedia;
import com.xuecheng.framework.domain.search.CourseSearchParam;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.search.service.EsCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author ShengJinFeng
 * @version V1.0
 * @ClassName EsCourseController
 * @Description (这里用一句话描述这个类的作用)
 * @date 2020/2/20 9:04
 * @since V1.0
 */
@RestController
@RequestMapping("/search/course")
@Api(value="课程搜索接口",description = "课程搜索接口")
public class EsCourseController implements EsCourseControllerApi {

    @Autowired
    private EsCourseService esCourseService;

    @Override
    @ApiOperation("课程搜索")
    @GetMapping(value="/list/{page}/{size}")
    public QueryResponseResult list(@PathVariable("page") int page,@PathVariable("size") int size, CourseSearchParam courseSearchParam) throws IOException {
        return esCourseService.list(page,size,courseSearchParam);
    }

    @Override
    @ApiOperation("添加课程文档")
    @PostMapping("add")
    public ResponseResult addEsCourse(@RequestBody CoursePub coursePub) {
        try {
            return esCourseService.addEsCourse(coursePub);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CommonCode.FAIL);
        }
    }

    @Override
    @ApiOperation("根据id查询课程信息")
    @GetMapping("/getall/{id}")
    public Map<String, CoursePub> getall(@PathVariable("id") String id) {
        return esCourseService.getall(id);
    }

    @Override
    @ApiOperation("根据课程计划id查询媒资信息")
    @GetMapping("/getmedia/{teachplanId}")
    public TeachplanMedia getmedia(@PathVariable("teachplanId") String teachplanId) {
        return esCourseService.getmedia(teachplanId);
    }

    @ApiOperation("添加课程计划媒资信息文档")
    @PostMapping("/teachplanMedia/add")
    public ResponseResult addEsTeachplanMedia(@RequestBody List<TeachplanMedia> list) {
        try {
            return esCourseService.addEsTeachplanMedia(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CommonCode.FAIL);
        }
    }


}
